package com.teach.javafxclient.controller.base;

import com.teach.javafxclient.Globalvar;
import com.teach.javafxclient.util.HttpUtil;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import kong.unirest.HttpResponse;
import kong.unirest.Unirest;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author Liang Jianlin
 * @date 2024-04-30 02:19
 */
public class SourceItemController {
    @FXML
    public Label type;
    @FXML
    public Label title;
    @FXML
    public Label time;
    @FXML
    public Label memSize;
    @FXML
    public VBox background;
    @FXML
    public Label ifDownloaded;
    public ImageView downLoad;
    public ImageView delete;
    private Integer resourceId;

    public void setItem(String fileType, String fileName, Integer resourceId) {
        type.setText(fileType);
        title.setText(fileName);
        this.resourceId = resourceId;
    }

    public void initialize() {
        downLoad.setOnMouseClicked(e -> {
            String downloadUrl = Globalvar.baseUrl + "/api/resources/" + resourceId; // 替换为你的下载链接
            String desktopPath = System.getProperty("user.home") + File.separator + "Desktop" + File.separator;//指向桌面的路径
            String saveFilePath = desktopPath + "smsFile" + File.separator + title.getText(); //默认的保存路径：桌面上的smsFile文件夹

            // 创建文件选择器
            DirectoryChooser directoryChooser = new DirectoryChooser();
            directoryChooser.setTitle("选择保存路径");

            File selectedDirectory = directoryChooser.showDialog(null);

            if (selectedDirectory != null) {
                String fileName =  title.getText();; // 替换为你想保存的文件名
                saveFilePath = selectedDirectory.getAbsolutePath() + File.separator + fileName;//// 替换为你想保存文件的路径和文件名
                }

                // 注意：确保路径存在或程序有权限创建它
                File saveFile = new File(saveFilePath);
                File parentDir = saveFile.getParentFile();

                if (!parentDir.exists()) {
                    // 如果目录不存在，则创建它
                    boolean dirCreated = parentDir.mkdirs();
                    if (!dirCreated) {
                        System.err.println("无法创建保存文件的目录。");
                        return;
                    }
                }

                try {
                    HttpResponse<byte[]> response = HttpUtil.get(downloadUrl).asBytes();

                    // 检查响应是否成功
                    if (response.getStatus() == 200) {
                        byte[] responseBody = response.getBody();

                        // 将字节数组写入到文件
                        try (FileOutputStream outputStream = new FileOutputStream(saveFile)) {
                            outputStream.write(responseBody);
                            System.out.println("文件下载完成并保存到: " + saveFilePath);
                            ifDownloaded.setText("已下载");
                        }
                    } else {
                        System.err.println("下载失败，HTTP状态码: " + response.getStatus());
                    }
                } catch (IOException e2) {
                    e2.printStackTrace();
                }
        });
    }
}